home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Docs / Sources / USectionMgr.cp < prev   
Encoding:
Text File  |  1996-04-03  |  19.2 KB  |  685 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // USectionMgr.cp
  3. // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6. #ifndef __USECTIONMGR__
  7. #include "USectionMgr.h"
  8. #endif
  9.  
  10. // MacApp
  11.  
  12. #ifndef __UADORNERS__
  13. #include "UAdorners.h"
  14. #endif
  15.  
  16. #ifndef __UAPPLEEVENTS__
  17. #include "UAppleEvents.h"
  18. #endif
  19.  
  20. //    #ifndef __UAPPLICATION__
  21. //    #include "UApplication.h"
  22. //    #endif
  23.  
  24. #ifndef __UBUSYCURSOR__
  25. #include "UBusyCursor.h"
  26. #endif
  27.  
  28. #ifndef __UCLIPBOARDMGR__
  29. #include "UClipboardMgr.h"
  30. #endif
  31.  
  32. #ifndef __UDEBUG__
  33. #include "UDebug.h"
  34. #endif
  35.  
  36. #ifndef __UDESKSCRAPVIEW__
  37. #include "UDeskScrapView.h"
  38. #endif
  39.  
  40. #ifndef __UDISPATCHER__
  41. #include "UDispatcher.h"
  42. #endif
  43.  
  44. #ifndef __UDOCUMENT__
  45. #include "UDocument.h"
  46. #endif
  47.  
  48. #ifndef __UEDITIONDOCUMENT__
  49. #include "UEditionDocument.h"
  50. #endif
  51.  
  52. #ifndef __UERRORMGR__
  53. #include "UErrorMgr.h"
  54. #endif
  55.  
  56. #ifndef __UFAILURE__
  57. #include "UFailure.h"
  58. #endif
  59.  
  60. #ifndef __UFILEBASEDDOCUMENT__
  61. #include "UFileBasedDocument.h"
  62. #endif
  63.  
  64. #ifndef __UFILE__
  65. #include "UFile.h"
  66. #endif
  67.  
  68. #ifndef __UMACAPPGLOBALS__
  69. #include "UMacAppGlobals.h"
  70. #endif
  71.  
  72. #ifndef __UMACAPPUTILITIES__
  73. #include "UMacAppUtilities.h"
  74. #endif
  75.  
  76. #ifndef __UMEMORY__
  77. #include "UMemory.h"
  78. #endif
  79.  
  80. #ifndef __UMENUMGR__
  81. #include "UMenuMgr.h"
  82. #endif
  83.  
  84. #ifndef __UPRINTHANDLER__
  85. #include "UPrintHandler.h"
  86. #endif
  87.  
  88. #ifndef __USCROLLER__
  89. #include "UScroller.h"
  90. #endif
  91.  
  92. #ifndef __USECTION__
  93. #include "USection.h"
  94. #endif
  95.  
  96. #ifndef __UVIEW__
  97. #include "UView.h"
  98. #endif
  99.  
  100. #ifndef __UWINDOW__
  101. #include "UWindow.h"
  102. #endif
  103.  
  104. // Toolbox
  105.  
  106. #ifndef __BALLOONS__
  107. #include <Balloons.h>
  108. #endif
  109.  
  110. #ifndef __ERRORS__
  111. #include <Errors.h>
  112. #endif
  113.  
  114. #ifndef __MENUS__
  115. #include <Menus.h>
  116. #endif
  117.  
  118. #ifndef __PACKAGES__
  119. #include <Packages.h>
  120. #endif
  121.  
  122. #ifndef __RESOURCES__
  123. #include <Resources.h>
  124. #endif
  125.  
  126. #ifndef __SCRAP__
  127. #include <Scrap.h>
  128. #endif
  129.  
  130. #ifndef __SCRIPT__
  131. #include <Script.h>
  132. #endif
  133.  
  134. #ifndef __TOOLUTILS__
  135. #include <ToolUtils.h>
  136. #endif
  137.  
  138. // ANSI
  139.  
  140. #ifndef __STDIO__
  141. #include <stdio.h>
  142. #endif
  143.  
  144. //----------------------------------------------------------------------------------------
  145. // private global variables
  146. Boolean pSectionMgrInitialized;
  147.  
  148.  
  149. //========================================================================================
  150. // CLASS TSectionMgr
  151. //========================================================================================
  152. #undef Inherited
  153. #define Inherited TBehavior
  154.  
  155. #pragma segment MAInit
  156. MA_DEFINE_CLASS_M1(TSectionMgr, Inherited);
  157.  
  158. //----------------------------------------------------------------------------------------
  159. // TSectionMgr::TSectionMgr:
  160. //----------------------------------------------------------------------------------------
  161. #pragma segment ConstructorRes
  162.  
  163. TSectionMgr::TSectionMgr()
  164. {
  165.     fSectionList = NULL;
  166. } // TSectionMgr::TSectionMgr
  167.  
  168. //----------------------------------------------------------------------------------------
  169. // TSectionMgr::ISectionMgr: 
  170. //----------------------------------------------------------------------------------------
  171. #pragma segment MAInit
  172.  
  173. void TSectionMgr::ISectionMgr()
  174.  
  175. {
  176.     this->IBehavior(kSectionMgrBehaviorID);
  177.  
  178.     FailInfo fi;
  179.     Try(fi)
  180.     {
  181.         TSectionList* aSectionList = new TSectionList;
  182.         aSectionList->ISectionList();
  183.         fSectionList = aSectionList;
  184. #if qDebug
  185.         fSectionList->SetEltType("TSection");    // takes MAName
  186. #endif
  187.         fi.Success();
  188.     }
  189.     else
  190.     {
  191.         this->Free();
  192.         fi.ReSignal();
  193.     }
  194.     
  195. } // TSectionMgr::ISectionMgr 
  196.  
  197. //----------------------------------------------------------------------------------------
  198. // TSectionMgr::Free: 
  199. //----------------------------------------------------------------------------------------
  200. #pragma segment MAClose
  201.  
  202. TSectionMgr::~TSectionMgr()
  203.  
  204. {
  205.     fSectionList = (TSectionList *) FreeIfObject(fSectionList);
  206.     // the preceding should be FreeListIfObject ... NOT!
  207. } // TSectionMgr::Free 
  208.  
  209. //----------------------------------------------------------------------------------------
  210. // TSectionMgr::DoAESectionEvent: 
  211. //----------------------------------------------------------------------------------------
  212. #pragma segment MAApplicationRes
  213.  
  214. void TSectionMgr::DoAESectionEvent(CommandNumber aCommandNumber,
  215.                                  const AppleEvent& message,
  216.                                  const AppleEvent& reply)
  217. {
  218.     TSectionCommand* theCommand = NULL;
  219.  
  220.     switch (aCommandNumber)
  221.     {
  222.         case cSectionRead:
  223.             theCommand = new TSectionReadEventCommand;
  224.             break;
  225.  
  226.         case cSectionWrite:
  227.             theCommand = new TSectionWriteEventCommand;
  228.             break;
  229.  
  230.         case cSectionScroll:
  231.             theCommand = new TSectionScrollEventCommand;
  232.             break;
  233.  
  234.         case cSectionCancel:
  235.             theCommand = new TSectionCancelEventCommand;
  236.             break;
  237.     }
  238.  
  239.     if (theCommand)
  240.     {
  241.         theCommand->ISectionCommand(aCommandNumber, message, reply);
  242.         fOwner->PostCommand(theCommand);
  243.     }
  244. } // TSectionMgr::DoAESectionEvent 
  245.  
  246.  
  247. //========================================================================================
  248. // CLASS TSectionCommand
  249. //========================================================================================
  250. #undef Inherited
  251. #define Inherited TServerCommand
  252.  
  253. #pragma segment MASectionNonRes
  254. MA_DEFINE_CLASS_M1(TSectionCommand, Inherited);
  255.  
  256. //----------------------------------------------------------------------------------------
  257. // TSectionCommand::TSectionCommand: Empty constructor to satisfy the compiler.
  258. //----------------------------------------------------------------------------------------
  259. #pragma segment ConstructorRes
  260.  
  261. TSectionCommand::TSectionCommand()
  262. {
  263.     fSection = NULL;
  264. } // TSectionCommand::TSectionCommand
  265.  
  266. //----------------------------------------------------------------------------------------
  267. // TSectionCommand destructor
  268. //----------------------------------------------------------------------------------------
  269. #pragma segment MADestructorRes
  270.  
  271. TSectionCommand::~TSectionCommand()
  272. {
  273. }
  274.  
  275. //----------------------------------------------------------------------------------------
  276. // TSectionCommand::ISectionCommand: 
  277. //----------------------------------------------------------------------------------------
  278. #pragma segment MASectionNonRes
  279.  
  280. void TSectionCommand::ISectionCommand(CommandNumber itsCommandNumber,
  281.                                       TDocument* itsDocument,
  282.                                       TSection* itsSection)
  283.  
  284. {
  285.     this->IServerCommand(itsCommandNumber, itsDocument, kCantUndo, kDoesNotCauseChange, itsDocument);
  286.  
  287.     fSection = itsSection;
  288. } // TSectionCommand::ISectionCommand 
  289.  
  290. //----------------------------------------------------------------------------------------
  291. // TSectionCommand::ISectionCommand: 
  292. //----------------------------------------------------------------------------------------
  293. #pragma segment MACommandRes
  294.  
  295. void TSectionCommand::ISectionCommand(CommandNumber itsCommandNumber,
  296.                                       const AppleEvent& itsMessage,
  297.                                       const AppleEvent& itsReply)
  298.  
  299. {
  300.     IServerCommand(itsCommandNumber, gDispatcher, kCantUndo, kDoesNotCauseChange, NULL,
  301.                    itsMessage, itsReply);
  302.  
  303.     FailInfo fi;
  304.  
  305.     Try(fi)
  306.     {
  307.         // get the section handle 
  308.         SectionHandle aSectionHandle = fMessage->ReadSectionHandle(keyDirectObject);
  309. #if qDebug
  310.         if (gIntenseDebugging)
  311.             fprintf(stderr, "SectionHandle from AppleEvent = %ld \n", (long)aSectionHandle);
  312. #endif
  313.         
  314.         // find the section object
  315.         fSection = NULL;
  316.         {    // for correct failure handling
  317.             TSectionMgr* aSectionMgr =
  318.                 (TSectionMgr*) gDispatcher->GetBehaviorWithIdentifier(kSectionMgrBehaviorID);
  319.             CSectionIterator iter(aSectionMgr->fSectionList);
  320.             TSection* aSection;
  321.             for (aSection = iter.FirstSection(); iter.More(); aSection = iter.NextSection())
  322.             {
  323.                 if (aSection->fSectionHandle == aSectionHandle)
  324.                 {
  325.                     fSection = aSection;
  326.                     break;
  327.                 }
  328.             }
  329.         }
  330.         if (!fSection)
  331.             Failure(noErr, 0);    // silent failure
  332.         
  333.         // associate the command correctly 
  334.         fContext = fSection->fDocument;
  335. #if qDebug
  336.         if (!IsObject(fContext))
  337.         {
  338.             VerboseIsObject(fContext);
  339.             ProgramBreak("the section object's document appears to be corrupt");
  340.         }
  341. #endif
  342.     
  343.         // ensure that the section is registered 
  344.         FailOSErr(fSection->Register());
  345.         fi.Success();
  346.     }
  347.     else
  348.     {
  349.         this->Free();
  350.         fi.ReSignal();
  351.     }
  352. } // TSectionCommand::ISectionCommand 
  353.  
  354.  
  355. //========================================================================================
  356. // CLASS TSectionReadEventCommand
  357. //========================================================================================
  358. #undef Inherited
  359. #define Inherited TSectionCommand
  360.  
  361. #pragma segment MASectionNonRes
  362. MA_DEFINE_CLASS_M1(TSectionReadEventCommand, Inherited);
  363.  
  364. //----------------------------------------------------------------------------------------
  365. // TSectionReadEventCommand::TSectionReadEventCommand: Empty constructor to satisfy the compiler.
  366. //----------------------------------------------------------------------------------------
  367. #pragma segment ConstructorRes
  368.  
  369. TSectionReadEventCommand::TSectionReadEventCommand()
  370. {
  371. } // TSectionReadEventCommand::TSectionReadEventCommand
  372.  
  373. //----------------------------------------------------------------------------------------
  374. // TSectionReadEventCommand destructor
  375. //----------------------------------------------------------------------------------------
  376. #pragma segment MADestructorRes
  377.  
  378. TSectionReadEventCommand::~TSectionReadEventCommand()
  379. {
  380. }
  381.  
  382. //----------------------------------------------------------------------------------------
  383. // TSectionReadEventCommand::ISectionReadEventCommand: 
  384. //----------------------------------------------------------------------------------------
  385. #pragma segment MASectionNonRes
  386.  
  387. void TSectionReadEventCommand::ISectionReadEventCommand(CommandNumber itsCommandNumber,
  388.                                                        TDocument* itsDocument,
  389.                                                        TSubscriber* itsSubscriber)
  390.  
  391. {
  392.     this->ISectionCommand(itsCommandNumber, itsDocument, itsSubscriber);
  393.  
  394.     fCausesChange = kCausesChange;
  395. } // TSectionReadEventCommand::ISectionReadEventCommand 
  396.  
  397. //----------------------------------------------------------------------------------------
  398. // TSectionReadEventCommand::DoIt: 
  399. //----------------------------------------------------------------------------------------
  400. #pragma segment MADoCommand
  401.  
  402. void TSectionReadEventCommand::DoIt()    // override 
  403. {
  404. //    FailInfo fi;
  405. //    Try(fi)
  406. //    {
  407.         TSubscriber * aSubscriber = (TSubscriber *)fSection;
  408.     
  409.         if ((!aSubscriber->fDocument->fStopAllEditions) && (aSubscriber->GetUpdateMode() == sumAutomatic))
  410.             aSubscriber->Subscribe();
  411.             
  412. //        fi.Success();
  413. //    }
  414. //    else
  415. //    {
  416. //        this->SetValidationError(fi.error);
  417. //
  418. //        fi.ReSignal();
  419. //    }
  420. } // TSectionReadEventCommand::DoIt 
  421.  
  422.  
  423. //========================================================================================
  424. // CLASS TSectionWriteEventCommand
  425. //========================================================================================
  426. #undef Inherited
  427. #define Inherited TSectionCommand
  428.  
  429. #pragma segment MASectionNonRes
  430. MA_DEFINE_CLASS_M1(TSectionWriteEventCommand, Inherited);
  431.  
  432. //----------------------------------------------------------------------------------------
  433. // TSectionWriteEventCommand::TSectionWriteEventCommand: Empty constructor to satisfy the compiler.
  434. //----------------------------------------------------------------------------------------
  435. #pragma segment ConstructorRes
  436.  
  437. TSectionWriteEventCommand::TSectionWriteEventCommand()
  438. {
  439. } // TSectionWriteEventCommand::TSectionWriteEventCommand
  440.  
  441. //----------------------------------------------------------------------------------------
  442. // TSectionWriteEventCommand destructor
  443. //----------------------------------------------------------------------------------------
  444. #pragma segment MADestructorRes
  445.  
  446. TSectionWriteEventCommand::~TSectionWriteEventCommand()
  447. {
  448. }
  449.  
  450. //----------------------------------------------------------------------------------------
  451. // TSectionWriteEventCommand::ISectionWriteEventCommand: 
  452. //----------------------------------------------------------------------------------------
  453. #pragma segment MASectionNonRes
  454.  
  455. void TSectionWriteEventCommand::ISectionWriteEventCommand(CommandNumber itsCommandNumber,
  456.                                                          TDocument* itsDocument,
  457.                                                          TPublisher* itsPublisher)
  458.  
  459. {
  460.     this->ISectionCommand(itsCommandNumber, itsDocument, itsPublisher);
  461. } // TSectionWriteEventCommand::ISectionWriteEventCommand 
  462.  
  463. //----------------------------------------------------------------------------------------
  464. // TSectionWriteEventCommand::DoIt: 
  465. //----------------------------------------------------------------------------------------
  466. #pragma segment MADoCommand
  467.  
  468. void TSectionWriteEventCommand::DoIt()    // override 
  469. {
  470. //    FailInfo fi;
  471. //    Try(fi)
  472. //    {
  473.     TFile* itsFile = NULL;
  474.     TFileBasedDocument* aFileBasedDocument = fSection->fDocument;
  475.     if (aFileBasedDocument && aFileBasedDocument->FileExists())
  476.         itsFile = aFileBasedDocument->GetFile();
  477.     
  478.     ((TPublisher *)fSection)->Publish(itsFile);
  479.     
  480. //        fi.Success();
  481. //    }
  482. //    else
  483. //    {
  484. //        this->SetValidationError(fi.error);
  485. //
  486. //        fi.ReSignal();
  487. //    }
  488.  
  489. } // TSectionWriteEventCommand::DoIt 
  490.  
  491.  
  492. //========================================================================================
  493. // CLASS TSectionScrollEventCommand
  494. //========================================================================================
  495. #undef Inherited
  496. #define Inherited TSectionCommand
  497.  
  498. #pragma segment MASectionNonRes
  499. MA_DEFINE_CLASS_M1(TSectionScrollEventCommand, Inherited);
  500.  
  501. //----------------------------------------------------------------------------------------
  502. // TSectionScrollEventCommand::TSectionScrollEventCommand: Empty constructor to satisfy the compiler.
  503. //----------------------------------------------------------------------------------------
  504. #pragma segment ConstructorRes
  505.  
  506. TSectionScrollEventCommand::TSectionScrollEventCommand()
  507. {
  508. } // TSectionScrollEventCommand::TSectionScrollEventCommand
  509.  
  510. //----------------------------------------------------------------------------------------
  511. // TSectionScrollEventCommand destructor
  512. //----------------------------------------------------------------------------------------
  513. #pragma segment MADestructorRes
  514.  
  515. TSectionScrollEventCommand::~TSectionScrollEventCommand()
  516. {
  517. }
  518.  
  519. //----------------------------------------------------------------------------------------
  520. // TSectionScrollEventCommand::ISectionScrollEventCommand: 
  521. //----------------------------------------------------------------------------------------
  522. #pragma segment MASectionNonRes
  523.  
  524. void TSectionScrollEventCommand::ISectionScrollEventCommand(CommandNumber itsCommandNumber,
  525.                                                            TDocument* itsDocument,
  526.                                                            TSection* itsSection)
  527.  
  528. {
  529.     this->ISectionCommand(itsCommandNumber, itsDocument, itsSection);
  530. } // TSectionScrollEventCommand::ISectionScrollEventCommand 
  531.  
  532. //----------------------------------------------------------------------------------------
  533. // TSectionScrollEventCommand::DoIt: 
  534. //----------------------------------------------------------------------------------------
  535. #pragma segment MADoCommand
  536.  
  537. void TSectionScrollEventCommand::DoIt()    // override 
  538. {
  539.     // at this point, the application has received and processed the 'odoc' AppleEvent 
  540.     // so, the window containing the section should be frontmost
  541.  
  542. //    FailInfo fi;
  543. //    Try(fi)
  544. //    {
  545.     // 1. bring the application to front (using the Process Manager) 
  546.     gDispatcher->MakeFrontProcess();
  547.  
  548.     // 2. tell the document to reveal the selection and select it
  549.     if (fContext)
  550.         ((TEditionDocument*)fContext)->RevealSelection(fSection->fDesignator);
  551.         
  552. //        fi.Success();
  553. //    }
  554. //    else
  555. //    {
  556. //        this->SetValidationError(fi.error);
  557. //
  558. //        fi.ReSignal();
  559. //    }
  560. } // TSectionScrollEventCommand::DoIt 
  561.  
  562.  
  563. //========================================================================================
  564. // CLASS TSectionCancelEventCommand
  565. //========================================================================================
  566. #undef Inherited
  567. #define Inherited TSectionCommand
  568.  
  569. #pragma segment MASectionNonRes
  570. MA_DEFINE_CLASS_M1(TSectionCancelEventCommand, Inherited);
  571.  
  572. //----------------------------------------------------------------------------------------
  573. // TSectionCancelEventCommand::TSectionCancelEventCommand: Empty constructor to satisfy the compiler.
  574. //----------------------------------------------------------------------------------------
  575. #pragma segment ConstructorRes
  576.  
  577. TSectionCancelEventCommand::TSectionCancelEventCommand()
  578. {
  579. } // TSectionCancelEventCommand::TSectionCancelEventCommand
  580.  
  581. //----------------------------------------------------------------------------------------
  582. // TSectionCancelEventCommand destructor
  583. //----------------------------------------------------------------------------------------
  584. #pragma segment MADestructorRes
  585.  
  586. TSectionCancelEventCommand::~TSectionCancelEventCommand()
  587. {
  588.     if (fCommandDone && (fSection != NULL))
  589.     {
  590.         fSection->Delete();
  591.         fSection = (TSection*) FreeIfObject(fSection);
  592.     }
  593. }
  594.  
  595. //----------------------------------------------------------------------------------------
  596. // TSectionCancelEventCommand::ISectionCancelEventCommand: 
  597. //----------------------------------------------------------------------------------------
  598. #pragma segment MASectionNonRes
  599.  
  600. void TSectionCancelEventCommand::ISectionCancelEventCommand(CommandNumber itsCommandNumber,
  601.                                                            TDocument* itsDocument,
  602.                                                            TSection* itsSection)
  603.  
  604. {
  605.     this->ISectionCommand(itsCommandNumber, itsDocument, itsSection);
  606.  
  607.     fCanUndo = kCanUndo;        // can undo the cancel
  608.     fCausesChange = kCausesChange;
  609. } // TSectionCancelEventCommand::ISectionCancelEventCommand 
  610.  
  611. //----------------------------------------------------------------------------------------
  612. // TSectionCancelEventCommand::DoIt: 
  613. //----------------------------------------------------------------------------------------
  614. #pragma segment MADoCommand
  615.  
  616. void TSectionCancelEventCommand::DoIt()    // override 
  617. {
  618. //    FailInfo fi;
  619. //    Try(fi)
  620. //    {
  621.         fSection->CancelSection(kCancel);
  622.         ((TEditionDocument*)fContext)->DoDeleteBorder(fSection);
  623. //        fi.Success();
  624. //    }
  625. //    else
  626. //    {
  627. //        this->SetValidationError(fi.error);
  628. //        fi.ReSignal();
  629. //    }
  630. } // TSectionCancelEventCommand::DoIt 
  631.  
  632. //----------------------------------------------------------------------------------------
  633. // TSectionCancelEventCommand::UndoIt: 
  634. //----------------------------------------------------------------------------------------
  635. #pragma segment MADoCommand
  636.  
  637. void TSectionCancelEventCommand::UndoIt()    // override 
  638. {
  639. //    FailInfo fi;
  640. //    Try(fi)
  641. //    {
  642.         fSection->CancelSection(!kCancel);
  643.         ((TEditionDocument *)fContext)->DoAddBorder(fSection);
  644. //        fi.Success();
  645. //    }
  646. //    else
  647. //    {
  648. //        this->SetValidationError(fi.error);
  649. //        fi.ReSignal();
  650. //    }
  651. } // TSectionCancelEventCommand::UndoIt 
  652.  
  653. //----------------------------------------------------------------------------------------
  654. // TSectionCancelEventCommand::Commit: 
  655. //----------------------------------------------------------------------------------------
  656.  
  657.  
  658. //========================================================================================
  659. // GLOBAL Procedures
  660. //========================================================================================
  661. #undef Inherited
  662.  
  663. //----------------------------------------------------------------------------------------
  664. // InitUSectionMgr: 
  665. //----------------------------------------------------------------------------------------
  666. #pragma segment MAInit
  667.  
  668. void InitUSectionMgr()
  669.  
  670. {
  671.     if (!pSectionMgrInitialized)
  672.     {
  673.         TSectionMgr * aSectionMgr = new TSectionMgr;    // instantiate a section mgr 
  674.         aSectionMgr->ISectionMgr();                        // initialize it 
  675.         gDispatcher->AddBehavior(aSectionMgr);            // give the application this behavior
  676.  
  677.         pSectionMgrInitialized = TRUE;
  678.     }
  679. } // InitUSectionMgr 
  680.  
  681. //----------------------------------------------------------------------------------------
  682. // End of USectionMgr.cp
  683.  
  684. #pragma segment Inline
  685.